<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 旧件状态修改 - 编辑页面 -->
    <!-- <BreakSpace label="批量修改" :containerStyle="{ marginBottom: '10px' }" /> -->
    <FormPanel ref="formPanel1" :list="formList" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 4rem"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 10px',
        background: '#fff',
        textAlign: 'right',
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
  </div>
  </Spin>
</template>
<script>
import { batchModification } from '@/api/usedPartsReturnManage/usedPartsReturn_OEM/usedPartsStatusUpdate';
import { dictionary } from '@/utils/dictMixin';
import { getPartType } from '@/api/dictionary/index.js';
export default {
  props: ['rowData'],
  mixins: [dictionary],
  data() {
    return {
      loading: false,
      formList: this.createFormList(),
      ids: '',
      cmsClaimIds: '',
      saveData: null
    };
  },
  async created() {
    await this.chuqi();
    await this.chuqi2();
  },
  methods: {
    async chuqi() {
      const res = await getPartType({ type: 5522 });
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'partStatus').initialValue = '';
        this.formList.find(x => x.fieldName == 'partStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    async chuqi2() {
      const res = await getPartType({ type: 5590 });
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'inInfo').initialValue = '';
        this.formList.find(x => x.fieldName == 'inInfo').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if(this.saveData) {
        if(this.rowData && this.rowData.length > 0) {
          this.loading = true;
          this.rowData.map(x => {
            x.partStatus = this.saveData.partStatus;
            if(this.saveData.inInfo) {
              x.inInfo = this.saveData.inInfo;
            } else {
              x.inInfo = undefined;
            }
          })
          let res = await batchModification(this.rowData);
          if (res.resultCode === 200) {
            this.$emit('save');
            return this.$notify.success({title: '提示信息', message: '保存成功'});
          } else {
            this.loading = false;
            this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
          }
        }
      }
    },
    removeCommas(str) {
      str = (str.substring(str.length - 1) == ',') ? str.substring(0, str.length - 1) : str;
      return str;
    },
    closeDialog() {
      this.$emit('close');
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '旧件状态',
          itemList: [],
          fieldName: 'partStatus',
          rules: [
            {
              required: true,
              message: '请选择旧件状态',
              trigger: 'change'
            }
          ],
          change: val => {
            if (val === 55221002) {
              this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = true;
            } else {
              this.formList.find(item => item.fieldName === 'inInfo').rules[0].required = false;
              this.formList.find(item => item.fieldName === 'inInfo').initialValue = undefined;
            }
          }
        },
        {
          type: 'SELECT',
          label: '不合格原因',
          itemList: [],
          fieldName: 'inInfo',
          rules: [
            {
              required: false,
              message: '请选择不合格原因',
              trigger: 'change'
            }
          ]
        },
      ];
    }

  }
};
</script>
<style lang="less" scoped></style>
